package org.orzeszek.transfer.gui;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class SettingsWindow extends JDialog
{
  public static final int DEFAULT_W = 320;
  public static final int DEFAULT_H = 120;

  private JLabel label_port;
  private JLabel label_address;

  private JTextField field_port;
  private JTextField field_address;

  public SettingsWindow( Frame owner )
  {
    super( owner );
  }

  public void initialize( Object aParent )
  {
    this.setLayout( new GridBagLayout() );

    this.label_port = new JLabel( "Port: " );
    this.label_port.setHorizontalAlignment( SwingConstants.RIGHT );
    this.label_address = new JLabel( "IP Address: " );

    this.field_port = new JTextField();
    this.field_port.setHorizontalAlignment( SwingConstants.RIGHT );
    this.field_address = new JTextField();

    GridBagConstraints label_port_c = new GridBagConstraints();
    label_port_c.gridx = 0;
    label_port_c.gridy = 0;
    label_port_c.fill = GridBagConstraints.HORIZONTAL;
    label_port_c.insets = new Insets( 0, Constants.PADDING, 0, 0 );
    this.add( this.label_port, label_port_c );

    GridBagConstraints field_port_c = new GridBagConstraints();
    field_port_c.gridx = 1;
    field_port_c.gridy = 0;
    field_port_c.fill = GridBagConstraints.HORIZONTAL;
    field_port_c.weightx = 1.0;
    field_port_c.insets = new Insets( 0, 0, 0, Constants.PADDING );
    this.add( this.field_port, field_port_c );

    GridBagConstraints label_address_c = new GridBagConstraints();
    label_address_c.gridx = 0;
    label_address_c.gridy = 1;
    label_address_c.fill = GridBagConstraints.HORIZONTAL;
    label_address_c.insets = new Insets( 0, Constants.PADDING, 0, 0 );
    this.add( this.label_address, label_address_c );

    GridBagConstraints field_address_c = new GridBagConstraints();
    field_address_c.gridx = 1;
    field_address_c.gridy = 1;
    field_address_c.fill = GridBagConstraints.HORIZONTAL;
    field_address_c.weightx = 1.0;
    field_address_c.insets = new Insets( 0, 0, 0, Constants.PADDING );
    this.add( this.field_address, field_address_c );

    this.setResizable( false );
  }

  /**
   * Positions the {@link ApplicationWindow} in the center of the screen. This
   * method should be called once the size of the frame has been set.
   */
  public void center()
  {
    Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();

    int w = this.getSize().width;
    int h = this.getSize().height;

    int x = ( screen.width - w ) / 2;
    int y = ( screen.height - h ) / 2;

    this.setLocation( x, y );
  }
}
